﻿using BBICMS.Model;
using BBICMS.Repository;
using BBICMS.WebUI.Filters;
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace BBICMS.WebUI.Areas.Admin.Controllers.api
{
        [SIGAuth]
    public class AdController : ApiController
    {
        public ApplicationUnit _unit = new ApplicationUnit();

        [HttpGet]
        [AllowAnonymous]
        public IEnumerable<Advertisement> Get()
        {
            return this._unit.Advertisements.GetAll();
        }



        [HttpGet]
        [ActionName("Edit")]
        [AllowAnonymous]
        [System.Web.Http.Authorize(Roles = "Administrators, Users")]
        public Advertisement Get(int id)
        {
            Advertisement ad = this._unit.Advertisements.GetById(id);
            if (ad == null)
            {
                throw new HttpResponseException(
                    Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return ad;
        }
        [HttpPut]
        [AllowAnonymous]
        // [System.Web.Http.Authorize(Roles = "Administrators, Users")]
        public HttpResponseMessage Put(int id, Advertisement ad)
        {
            if (!ModelState.IsValid)
            {
                return Request.
                    CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != ad.Id)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            Advertisement existingAd = this._unit.Advertisements.GetById(id);
            _unit.Advertisements.Detach(existingAd);

            this._unit.Advertisements.Update(ad);

            try
            {
                this._unit.SaveChanges();

                // Return an explicit value to avoid the fail callback
                // being incorrectly invoked.
                return Request.
                    CreateResponse(HttpStatusCode.OK,
                    "{success:'true', verb:'PUT'}");
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.
                    CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
            catch (Exception ex)
            {
                return Request.
                    CreateErrorResponse(HttpStatusCode.BadRequest, ex);
            }
        }

        [HttpPost]
        [System.Web.Http.Authorize(Roles = "Administrators, Users")]
        public HttpResponseMessage Post(Advertisement ad)
        {
            try
            {
                //if (ModelState.IsValid)
                //{

                    this._unit.Advertisements.Add(ad);
                    this._unit.SaveChanges();

                    HttpResponseMessage result =
                        Request.CreateResponse(HttpStatusCode.Created, ad);

                    result.Headers.Location =
                        new Uri(Url.Link("DefaultApi", new { id = ad.Id }));

                    return result;
                //}
                //else
                //{
                //    return Request.
                //        CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
                //}
            }
            catch (Exception ex)
            {
                return Request.
                    CreateErrorResponse(HttpStatusCode.BadRequest, ex);
            }
        }

        [HttpDelete]
        [System.Web.Http.Authorize(Roles = "Administrators, Users")]
        public HttpResponseMessage Delete(int id)
        {
            Advertisement ad = this._unit.Advertisements.GetById(id);

            if (ad == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            this._unit.Advertisements.Delete(ad);

            try
            {
                this._unit.SaveChanges();
                return Request.CreateResponse(HttpStatusCode.OK, ad);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.
                    CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
        }

        protected override void Dispose(bool disposing)
        {
            this._unit.Dispose();
            base.Dispose(disposing);
        }
    }
}
